package com.yxx.community.service;

import com.yxx.community.mapper.UserMapper;
import com.yxx.community.model.User;
import com.yxx.community.model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: YuXinXin
 * @Description:
 * @Date:Created in 16:05 2020/2/3
 **/
@Service
public class UserService {
  @Autowired
  UserMapper userMapper;

  public void createOrUpdate(User user) {
    UserExample userExample = new UserExample();
    userExample.createCriteria().andAccountIdEqualTo(user.getAccountId());
    List<User> users = userMapper.selectByExample(userExample);
    if(users.size()==0){
      //插入
      user.setGmtCreate(System.currentTimeMillis());
      user.setGmtModified(user.getGmtCreate());
      userMapper.insert(user);
    }else{
      //更新
      User dbUser = users.get(0);
      User updateUser = new User();
      updateUser.setGmtModified(System.currentTimeMillis());
      updateUser.setAvatarUrl(user.getAvatarUrl());
      updateUser.setName(user.getName());
      updateUser.setToken(user.getToken());
      //这个方法是传过来的值不为空的时候才放进去的
      UserExample example = new UserExample();
      example.createCriteria().andIdEqualTo(dbUser.getId());
      userMapper.updateByExampleSelective(updateUser, example);
    }
  }
}
